package com.hulk.dryad.manage.framework.storage.config;

import com.hulk.dryad.manage.boot.upload2.DryadUploadProperties;
import com.hulk.dryad.manage.framework.s3.template.FsTemplate;
import com.hulk.dryad.manage.framework.storage.CloudStorage;
import com.hulk.dryad.manage.framework.storage.LocalStorage;
import com.hulk.dryad.manage.framework.storage.Storage;
import com.hulk.dryad.manage.framework.storage.StorageModeEnum;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.ObjectProvider;
import org.springframework.boot.context.properties.EnableConfigurationProperties;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

/**
 * @author hulk
 */
@Slf4j
@Configuration
@EnableConfigurationProperties(StorageProperties.class)
@RequiredArgsConstructor
public class StorageAutoConfiguration {

  private final DryadUploadProperties dryadUploadProperties;

  @Bean
  public Storage storageTemplate(ObjectProvider<FsTemplate> fsTemplate, StorageProperties storageProperties) {
    StorageModeEnum name = storageProperties.getName();

    if (StorageModeEnum.LOCAL.equals(name)) {
		LocalStorage  local = new LocalStorage(dryadUploadProperties,storageProperties);
		return local;
    } else if (StorageModeEnum.CLOUD.equals(name)) {
		CloudStorage  cloud = new CloudStorage(storageProperties,fsTemplate.getIfAvailable());
		return cloud;
    } else {
        log.warn("当前存储模式 " + name + " 不支持");
        return null;
    }
  }
}
